package com.pet.user.controller;


import com.pet.api.dto.AddressDTO;
import com.pet.common.domain.Result;
import com.pet.user.domain.dto.AddressFormDTO;
import com.pet.user.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户收货地址表 前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-07-03
 */
@Api(tags = "收货地址相关接口")
@RestController
@RequestMapping("/addresses")
@RequiredArgsConstructor
public class AddressController {
    private final IAddressService addressService;

    @GetMapping
    @ApiOperation("获取收货地址列表")
    public Result getAddresses() {
        return Result.success(addressService.getAddressesByUser());
    }

    @PostMapping
    @ApiOperation("添加收货地址")
    public Result addAddress(@RequestBody @Validated AddressFormDTO addressFormDTO) {
        boolean isSucceed = addressService.addAddressByUser(addressFormDTO);
        return isSucceed ? Result.success() : Result.error();
    }

    @PutMapping
    @ApiOperation("修改收货地址")
    public Result updateAddress(@RequestBody @Validated AddressFormDTO addressFormDTO) {
        if (addressFormDTO.getId() == null) {
            return Result.error("请正确提交信息!");
        }
        boolean isSucceed = addressService.updateAddressByUser(addressFormDTO);
        return isSucceed ? Result.success() : Result.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除收货地址")
    public Result deleteAddress(@PathVariable Integer id) {
        boolean isSucceed = addressService.deleteAddressByUser(id);
        return isSucceed ? Result.success() : Result.error();
    }

    @GetMapping("/order/{id}")
    @ApiOperation(value = "根据id查询收货地址", hidden = true)
    public AddressDTO queryAddressById(@PathVariable Integer id) {
        return addressService.queryAddressById(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询收货地址")
    public Result getAddressById(@PathVariable Integer id) {
        return addressService.getAddressById(id);
    }
}
